import { AV, request } from "./leanCloud";

const AsideMenu = new AV.Query("AsideMenu");
const HeadMenu = new AV.Query("HeadMenu");
const DropMenu = new AV.Query("DropMenu");
const Permission = new AV.Query("Permission");
const Route = new AV.Query("Route");

// 用户名密码登录
export function login({ username, password }) {
  // return request(AV.User.logIn(username, password));
  return AV.User.logIn(username, password);
}

// 注册
export function register({ username, password, email }) {
  return AV.User.signUp(username, password, {
    email
  });
}

// 重置密码
export function resetPassword(email) {
  return AV.User.requestPasswordReset(email);
}

// 注销
export function logout() {
  AV.User.logOut();
}

// 获取验证码url
export function captcha() {
  return AV.Captcha.request({
    width: 100,
    height: 50
  });
}

// 修改密码
export function updatePassword(oldPassword, newPassword) {
  return AV.User.updatePassword(oldPassword, newPassword);
}

// 获取角色列表
export function getRoles() {
  return AV.User.getRoles();
}

// 获取角色权限
export function getPermissons(roleName) {
  return Permission.equalTo("roleName", roleName);
}

// 获取菜单
export function getMenus(roleName) {
  return Promise.all([
    HeadMenu.equalTo("roleName", roleName),
    AsideMenu.equalTo("roleName", roleName),
    DropMenu.equalTo("roleName", roleName)
  ]);
}

// 获取路由
export function getRoutes(roleName) {
  return Route.equalTo("roleName", roleName);
}
